package com.muyu.danmo.service.impl;

import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCommunity;
import com.muyu.danmo.domain.DanmoCommunityUser;
import com.muyu.danmo.domain.DanmoUserFollows;
import com.muyu.danmo.mapper.DanmoCommunityUserMapper;
import com.muyu.danmo.mapper.DanmoUserFollowsMapper;
import com.muyu.danmo.service.IDanmoCommunityService;
import com.muyu.danmo.service.IDanmoCommunityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 社区用户Service业务处理层
 *
 * @author : kunzong
 * @date : 2024-11-19
 */
@Service
public class DanmoCommunityUserServiceImpl implements IDanmoCommunityUserService {
    @Autowired
    private DanmoCommunityUserMapper danmoCommunityUserMapper;

    @Autowired
    private DanmoUserFollowsMapper danmoUserFollowsMapper;

    @Autowired
    private IDanmoCommunityService danmoCommunityService;


    /**
     * 根据id查询社区用户
     *
     * @param id
     * @return
     */
    @Override
    public DanmoCommunityUser selectDanmoCommunityUserById(Long id) {
        DanmoCommunityUser danmoCommunityUser = danmoCommunityUserMapper.selectDanmoCommunityUserById(id);
        if (danmoCommunityUser == null){
            danmoCommunityUser = new DanmoCommunityUser();
        }
        //        查询粉丝数量和关注数量
        {
            DanmoUserFollows danmoUserFollows = new DanmoUserFollows();
            danmoUserFollows.setFolloweeId(id);
//        粉丝
            List<DanmoUserFollows> danmoUserFollowsList = danmoUserFollowsMapper.selectDanmoUserFollowsfanList(danmoUserFollows);
            if (danmoUserFollowsList != null) {
                danmoCommunityUser.setFolloweeNumber((long) danmoUserFollowsList.size());
            } else {
                danmoCommunityUser.setFolloweeNumber(0L);
            }
        }

        {
            DanmoUserFollows danmoUserFollows = new DanmoUserFollows();
            danmoUserFollows.setFollowerId(id);
//        关注
            List<DanmoUserFollows> danmoUserFollowsList = danmoUserFollowsMapper.selectDanmoUserFollowsfanList(danmoUserFollows);
            if (danmoUserFollowsList != null) {
                danmoCommunityUser.setFollowerNumber((long) danmoUserFollowsList.size());
            } else {
                danmoCommunityUser.setFollowerNumber(0L);
            }
        }

        //获赞与收藏数
        DanmoCommunity danmoCommunity = new DanmoCommunity();
        danmoCommunity.setAuthorId(id);
        List<DanmoCommunity> danmoCommunities = danmoCommunityService.selectDanmoCommunityLists(danmoCommunity);
        Long count = 0L;
        for (DanmoCommunity community : danmoCommunities) {
            count += community.getLikes() + community.getCollects();
        }
        danmoCommunityUser.setLikeAndCollectNumber(count);


//        获取该用户是否已被关注
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null) {
            DanmoUserFollows danmoUserFollows = danmoUserFollowsMapper.selectDanmoUserFollowsByUserIdAndFollowId(id, loginUser.getUserid());
            if (danmoUserFollows != null) {
                danmoCommunityUser.setWhetherToFollow("已关注");
            }
        }
        return danmoCommunityUser;
    }
}
